***********************************************************
*** Figure 8: Location Effects vs. Implicit Subsidies

	import delimited using "${mypath}/intermediate_csv/govtpolicy01-binscatter_fe_cz_gaf_diff.csv", clear asdoub

	loc beta: di %04.3fc fe_cz_diff_slope[1]  
	local beta_se: di %04.3fc fe_cz_diff_slope_se[1]
	local betaval = fe_cz_diff_slope[1]
	local r2: di %04.3fc fe_cz_diff_r2[1]
	loc cons = fe_cz_diff_cons[1]
	local xm = xmean[1]

	# d; 
	tw  (scatter fe_cz_physicians diff_gaf_laspeyres, mcolor("8 48 107"))  
		(function y = `cons'+`betaval'*x, range(-0.2 .2) lcolor(eltblue)),
		legend(off) 
		xtitle("Implicit Subsidy: Log(GAF / Price Index)", size(medlarge))
		xlabel(-.2(.1).2, nogrid labsize(medlarge))
		ylabel(, labsize(medlarge) nogrid)
		ytitle("") 
		text(0.1 .15 "{&beta} = `beta' (`beta_se')", color(black) size(medium) justification(left)  placement(n) linegap(1))
		text(0.075 .129 "R{sup:2} = `r2'", color(black) size(medium) justification(left)  placement(n) linegap(1))
		ymtick(##2)
		title("Relative Physician Earnings" "(CZ Fixed Effects)", size(medlarge) placement(9) span justification(left) col(black))
		;
	# d cr
	graph export "${individ_figs}/govtpolicy01-binscatter_fe_cz_gaf_diff.eps", replace 

***********************************************************
***  Table 3: RVU Regression Table

	import delimited using "${mypath}/intermediate_csv/govtpolicy01-RVU_Regressions.csv", asdoub clear
	keep if !inlist(depvar,"log_q_fix","log_maxmcrpay","log_minmcrpay")
	
	* NPI-level FE-estimates
	replace depvar = "1.\shortstacking{Log \\ Income}" if depvar == "logptotinc" & indepvar == "log_rvus_iv_atfixq"
	replace depvar = "2.\shortstacking{Log Total \\ RVUs Billed}" if depvar == "log_rvus" & indepvar == "log_rvus_iv_atfixq"
	replace depvar = "3.\shortstacking{Log Number of \\ Unique Procedures}" if depvar == "log_unique_procedures"

	* Procedure-level FE-estimates 
	replace depvar = "4.\shortstacking{Log Number of \\ Unique Patients}" if depvar == "log_patients"
	replace depvar = "5.\shortstacking{Log Total \\ RVUs Billed}" if depvar == "log_rvus" & indepvar == "log_rvu_value"
	
	* 2SLS-Estimations NPI
	replace depvar = "6.\shortstacking{Log \\ Income}" if depvar == "logptotinc" & indepvar == "log_rvus"
	replace depvar = "7.Retired" if depvar == "retired_1099ssa"  & indepvar == "logptotinc"
	
	* Independent variable
	replace indepvar = "Log Medicare Price Instrument" if indepvar == "log_rvus_iv_atfixq"
	replace indepvar = "Log RVUs per Procedure" if indepvar == "log_rvu_value"
	replace indepvar = "Log Total Number of RVUs Billed" if indepvar == "log_rvus"
	replace indepvar = "Log Individual Total Income" if indepvar == "logptotinc"

	sort agerange depvar
	
	* Format data
	foreach v of varl obs estimate se mean* sd* {
	
		if inlist("`v'","obs") {
			tostring `v', format("%12.0fc") replace force
		}
		if inlist("`v'","se","estimate") {
			replace `v' = round(`v', 0.001)
			tostring `v', format("%05.3f") replace force
			if "`v'" == "se" replace `v' = "(" + `v' + ")"
			
		}
		if inlist("`v'","mean_depvar","mean_indepvar","sd_indepvar","sd_depvar"){
			replace `v' = round(`v', 0.01)
			tostring `v', format("%04.2fc") replace force
		}
	}
	
	* Table header
	texdoc init "${tables}/govtpolicy01-RVU_Regressions.tex", replace
		
	loc tableno " "
	loc tabspec "l"		
	
	foreach i of numl 1(1)`=_N/2' {
		loc tabspec "`tabspec'c"
		loc tableno "`tableno' & (`i')"		
	}		
				 
	tex \begin{table}[h!] 
	tex \caption{\bf RVU Regression Table \label{tab:rvutable}} 
	tex \begin{center}
	tex \resizebox{1\textwidth}{!}{
	tex \begin{tabular}{`tabspec'}  \midrule \midrule 
	tex & \multicolumn{3}{c}{NPI-Level} & \multicolumn{2}{c}{Procedure-level} & \multicolumn{2}{c}{2SLS} \\ 
	tex \cmidrule(lr){2-4} \cmidrule(lr){5-6} \cmidrule(lr){7-8} 
	tex `tableno' \\ \textbf{Dependent variable:} 
	levelsof depvar 
	foreach level in `r(levels)' {
		tex & `=substr("`level'", 3, .)'
	}
	tex \\ \midrule

	levelsof agerange
	foreach age in `r(levels)' {
		preserve
		keep if agerange == "`age'"
		
		if "`age'" == "age40to55" 	tex \addlinespace[1ex] \multicolumn{8}{l}{\textbf{Panel A: Physicians Age 40-55}}  \\  \cmidrule(lr){1-1} \addlinespace[1ex] 
		if "`age'" == "age56to70" 	tex \addlinespace[1ex] \multicolumn{8}{l}{\textbf{Panel B: Physicians Age 56-70}}  \\ \cmidrule(lr){1-1} \addlinespace[1ex] 
		
		tex Log Medicare Price Instrument ($ \ln P_{i,t} $)   & `=estimate[1]' & `=estimate[2]' & `=estimate[3]'  &    &   &   & \\	
		tex 		 				      & `=se[1]'       & `=se[2]'       & `=se[3]'        &    &   &   &  \\
		tex Log RVUs per Procedure ($ \ln RVU_{k,t} $) 	     					&   &   &   & `=estimate[4]' & `=estimate[5]' &  & \\	
		tex 	 									 	&   &   &   & `=se[4]'       & `=se[5]'       &  & \\
		tex Log Total RVUs Billed ($ \ln Q_{i,t} $)				  					&  &  &  &  &  &  `=estimate[6]' & \\	
		tex 				   							 			&  &  &  &  &  &  `=se[6]'       & \\
		tex Log Income 							  							&  &  &  &  &  &  &  `=estimate[7]' \\	
		tex 				 							  				&  &  &  &  &  &  &  `=se[7]' \\ \addlinespace[1ex]

		tex  Mean of Dependent Variable (2010-13) & `=mean_depvar[1]' & `=mean_depvar[2]' & `=mean_depvar[3]' & `=mean_depvar[4]' & `=mean_depvar[5]' & `=mean_depvar[6]' & `=mean_depvar[7]'  \\	
		tex  Std. Dev. of Dependent Variable (2010-13) & `=sd_depvar[1]' & `=sd_depvar[2]' & `=sd_depvar[3]' & `=sd_depvar[4]' & `=sd_depvar[5]' & `=sd_depvar[6]' & `=sd_depvar[7]'  \\
		tex  Mean of Independent Variable & `=mean_indepvar[1]' & `=mean_indepvar[2]' & `=mean_indepvar[3]' & `=mean_indepvar[4]' & `=mean_indepvar[5]' & 	`=mean_indepvar[6]' & `=mean_indepvar[7]'  \\
		tex  Std. Dev. of Independent Variable & `=sd_indepvar[1]' & `=sd_indepvar[2]' & `=sd_indepvar[3]' & `=sd_indepvar[4]' & `=sd_indepvar[5]' & `=sd_indepvar[6]' & `=sd_indepvar[7]'  \\
		tex  Number of Observations & `=obs[1]' & `=obs[2]' & `=obs[3]' & `=obs[4]' & `=obs[5]' & `=obs[6]' & `=obs[7]'  \\
		restore
	}

	tex \addlinespace[1ex] \hline \hline
	tex \end{tabular}
	tex }
	tex \end{center}
	tex \end{table}
	
	texdoc close

***********************************************************************
*** Figure E.9: Distribution of RVU Changes

	* Prepare data
	import delimited using "${mypath}/intermediate_csv/govtpolicy01-kden_changes_instrument.csv", clear asdoub

	loc barwidth = `=rvu_chg[2]' - `=rvu_chg[1]' 
	loc mean = `=mean_change_rvus[3]'
	loc tmean = string(round(`=mean_change_rvus[3]', .001),"%04.3f")
	loc sd = string(round(`=sd_change_rvus[3]',.001),"%04.3f")
	
	foreach var of varl p* {
		loc `var' = string(round(`=`var'[1]',.01),"%04.3f")
	}
	
	qui su frac 
	loc text_height = `r(max)' + `r(max)'/15	
	
	* Graph
	#d ;
	tw  (bar frac rvu_chg, bcolor("8 48 107%70") lw(none) barwidth(`barwidth') bstyle(histogram)),  
		ylab(, nogrid) ytitle("") yscale(r(0 `=`text_height' + (`text_height'/20)'))
		xlab(, nogrid)  
		xtitle("Change in Log Medicare Price Instrument ({&Delta} ln{it:P}{sub:i,t})") xline(`mean', lc(g8) noextend) 
		text(`text_height' `mean' " Mean = `tmean'" " SD = `sd'", placement(right) color(g8) size(small) justification(left) box bcolor(none))
		xline(`mean', lc(g8) noextend) 	
		title("Density", size(medium) placement(9) span justification(left) col(black))
		note(" " "5{sup:th} Percentile: `p5'; 25{sup:th} Percentile: `p25'; 50{sup:th} Percentile: `p50';" "75{sup:th} Percentile: `p75'; 95{sup:th} Percentile: `p95'", pos(5) size(small) linegap(1) justification(right)) ;
		;
	#d cr	
	
	graph export "${individ_figs}/govtpolicy01-kden_Dlog_rvus_iv_atfixq_doc_year_age40to55.eps", replace

***********************************************************************
*** Figure E.10: Effects of Changes in Medicare RVUs

	* Load data
	import delimited using "${mypath}/intermediate_csv/govtpolicy01-RVU_Regressions.csv", asdoub clear
	keep if inlist(depvar,"logptotinc","log_rvus","log_unique_procedures","log_rvus","log_patients") & agerange == "age40to55" | inlist(depvar,"logptotinc","retired_1099ssa")  & agerange == "age56to70" & inlist(indepvar,"log_rvus","logptotinc")
		
	* NPI-level FE-estimates
	replace depvar = "1.Log Income" if depvar == "logptotinc" & indepvar == "log_rvus_iv_atfixq"
	replace depvar = "2.Log Total RVUs Billed (ln {it:Q}{sub:i,t})" if depvar == "log_rvus" & indepvar == "log_rvus_iv_atfixq"
	replace depvar = "3.Log Number of Unique Procedures" if depvar == "log_unique_procedures"
	* Procedure-level FE-estimates 
	replace depvar = "5.Log Number of Unique Patients" if depvar == "log_patients" & level == "procedure-level"
	replace depvar = "6.Log Total RVUs Billed (ln {it:Q}{sub:i,t})" if depvar == "log_rvus" & indepvar == "log_rvu_value" & level == "procedure-level"
	* 2SLS-Estimations NPI
	replace depvar = "8.Log Income 40-55 Sample" if depvar == "logptotinc" & indepvar == "log_rvus" & agerange == "age40to55"
	replace depvar = "9.Log Income 56-70 Sample" if depvar == "logptotinc" & indepvar == "log_rvus" & agerange == "age56to70"
	replace depvar = "9.Probability of Retirement for 56-70 Sample" if depvar == "retired_1099ssa" & indepvar == "logptotinc"
	
	gsort depvar
	insobs 1, before(1)
	replace depvar = "0.Panel A" if depvar == ""

	insobs 1, before(5)
	replace depvar = "4.Panel B" if depvar == ""
	
	insobs 1, before(8)
	replace depvar = "7.Panel C" if depvar == ""
	
	sort depvar
	g y = -1* _n
	replace depvar = substr(depvar, 3, .)
	labmask y, val(depvar)
		
	* Coefplot of main estimates in RVU analysis
	levelsof y if !strpos(depvar,"Panel"), loc(coeflabs)
	#d ;
	tw (scatter  y estimate, lcolor("8 48 107"*0.8) mcolor("8 48 107") msymbol(o) msize(medium)) 
		(rcap min95 max95 y, lcolor("8 48 107"*0.8) lpatter(solid) horizontal lwidth(medium)), 
		ytitle("") xtitle("Estimate", size(medlarge))  
		ylabel(`coeflabs', labsize(medium) valuelabel) 
		ysc(r(-1 -11.5))
		legend(off) graphregion(color(white))
		xline(0, lwidth(0.1) lc(g12) lpattern(dash)) 
		yline(`coeflabs', lcolor(gs10) lpattern(dot) lwidth(thin)) 
		xline(-0.5(0.5)1.5, lcolor(gs10) lpattern(dot) lwidth(thin)) 
		xlab(-0.5(0.5)2, labsize(medium))
		xsize(7)
		graphregion(color(white) m(l+14 t+8 b=0))
		text(0 -2.8 "{bf:Dependent variable:}", place(e) size(medlarge)) 
		text(-1 -2.8 "{bf:Panel A: Physician-Level}", place(e) size(medium)) 
		text(-5 -2.8 "{bf:Panel B: Procedure-Level}", place(e) size(medium)) 
		text(-8 -2.8 "{bf:Panel C: Physician-Level (2SLS)}", place(e) size(medium)) 
	;
	#d cr
	graph export "${individ_figs}/govtpolicy01-RVU_Regressions.eps", replace

**********************************************************************************
*** Figure E.12: Implicit subsidy vs. log median household income 

	import delimited using "${mypath}/intermediate_csv/govtpolicy01-binscatter_gaf_diff_med_hhinc2016.csv", clear asdoub 

	loc beta: di %04.3fc hhinc_slope[1]  
	local beta_se: di %04.3fc hhinc_slope_se[1]
	local betaval = hhinc_slope[1]
	local r2: di %04.3fc hhinc_r2[1]
	loc cons = hhinc_cons[1]
	local xm = xmean[1]
	
	# d; 
		tw (scatter diff_gaf_laspeyres ln_med_hhinc2016, mcolor("8 48 107"))
		(function y = `cons'+`betaval'*x, range(10.4 11.25) lcolor(eltblue)),
		legend(off) 
		xtitle("Log Median Household Income", size(medlarge))
		xlabel(, nogrid format(%9.1fc) labsize(medlarge))
		ylabel(, labsize(medlarge) nogrid format(%9.2fc))
		ytitle("",) 
		text(0 11.1 "{&beta} = `beta' (`beta_se')", color(black) size(medium) justification(left)  placement(n) linegap(1))
		text(-0.014 11.042 "R{sup:2} = `r2'", color(black) size(medium) justification(left)  placement(n) linegap(1))
		ymtick(##2)
		title("Implicit Subsidy: Log(GAF / Price Index)", size(medlarge) placement(9) span justification(left) col(black))
		;
	# d cr 	
	graph export "${individ_figs}/govtpolicy01-binscatter_gaf_diff_med_hhinc2016.eps", replace


************************************************************************************
*** Table E.10: Inputs to Analysis in Section 4.1

	import delimited using "${mypath}/intermediate_csv/govtpolicy01-cz_gaf_elasticity.csv", clear asdoub
	drop if v1 == "v1" | strpos(v1, "N_UR")
	
	* Formatting
	foreach v of varl *{
		replace `v' = subinstr(`v', "=", "", .)
		replace `v' = substr(`v', 2, strlen(`v')-2)
	}
	
	ds v1, not
	foreach var of varl `r(varlist)' {
		destring `var', gen(`var'_est) force
		destring `var', gen(`var'_se) force
		replace `var' = string(`var'_est,"%04.2f") if v1 != "" & v1 != "N" & _n > 2
		replace `var' =  "(" + string(`var'_se,"%04.2f")  + ")" if v1 == "" & _n > 2
		replace `var' = "" if `var'=="(.)"
	}
	drop *_est *_se
	
	texdoc init "${tables}/govtpolicy01-cz_gaf_elasticity.tex", replace	
	
	tex \begin{table}[h!] 
	tex \caption{\bf Inputs to Analysis in Section 4.1 \label{tab:gaf_analysis}} 
	tex \begin{center}
	tex \resizebox{1\textwidth}{!}{		
	tex \begin{tabular}{lcccc}  \midrule \midrule 
	tex & (1) & (2) & (3) & (4) \\
	
	tex & \multirow{2}{*}{`=v2[2]'} & \multirow{2}{*}{`=v3[2]'} & \multirow{2}{*}{`=v4[2]'} & \multirow{2}{*}{`=v5[2]'}  \\ 
	tex \\ \midrule
	
	forvalues k = 3/`=_N' {	
			
		tex `=v1[`k']'  & `=v2[`k']' & `=v3[`k']' & `=v4[`k']'  & `=v5[`k']' \\
		
		if "`=v1[`k']'"=="" tex \addlinespace[1ex]
	}
	
	tex \hline \hline
	tex \end{tabular}
	tex }
	tex \end{center}
	tex \end{table}

	texdoc close
